package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptor loginInterceptor;

    /**
     * 静态资源处理器
     */
   /* @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("进行静态资源映射。。。");
        registry.addResourceHandler("/backend/**")// 浏览器访问路径
                .addResourceLocations("classpath:/backend/");// 服务器资源存放路径
        registry.addResourceHandler("/front/**")
                .addResourceLocations("classpath:/front/");
    }*/

    /**
     * 拦截器配置
     * @param registry 添加拦截器配置,拦截路径,排除路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("添加拦截器配置。。。");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") // 拦截所有
                .excludePathPatterns("/employee/login","/backend/**","/front/**","/user/sendMsg","/user/login"); //放行静态资源
    }

    /**
     * 扩展Mvc框架的消息转换器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转换器..");
        //创建一个新的消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器,底层使用jackson将java对象转为JSON字符串
        messageConverter.setObjectMapper( new JacksonObjectMapper());
        //将上面的消息转换器追加到Mvc框架集合中,设置在0序列上
        converters.add(0,messageConverter);
    }
}
